package com.project.service.impl;

import com.project.bean.DeptBean;
import com.project.bean.EmployeeBean;
import com.project.mapper.IDeptMapper;
import com.project.service.IDeptService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class DeptServiceImpl extends BaseService implements IDeptService {
    @Override
    public void add(DeptBean dept, List<EmployeeBean> emList) {
        SqlSession session = this.getSession();
        IDeptMapper mapper = session.getMapper(IDeptMapper.class);
        //添加部门
        mapper.add(dept);
        //批量添加员工
        mapper.addEmployeeList(dept.getId(),emList);

        session.commit();
        session.close();
    }

    @Override
    public void delCaseCade(Integer deptId) {
        SqlSession session = this.getSession();
        try {
            IDeptMapper mapper = session.getMapper(IDeptMapper.class);
            mapper.delCaseCade(deptId);
            session.commit();
        }catch (Exception e){
            session.rollback();
        }finally {
            session.close();
        }
    }

    @Override
    public void delSetNull(Integer deptId) {
        SqlSession session = this.getSession();
        IDeptMapper mapper = session.getMapper(IDeptMapper.class);
        mapper.delSetNull(deptId);

        session.commit();
        session.close();
    }

    @Override
    public List<DeptBean> findAll() {
        SqlSession session = this.getSession();
        IDeptMapper mapper = session.getMapper(IDeptMapper.class);
        List<DeptBean> list = mapper.findAll();
        session.close();

        return list;
    }

    @Override
    public DeptBean findById(Integer id) {
        SqlSession session = this.getSession();
        IDeptMapper mapper = session.getMapper(IDeptMapper.class);
        DeptBean dept =  mapper.findById(id);
        session.close();

        return dept;
    }
}
